﻿@using OA.Model;
@using OA.Model.View;
@using OA.Model.Result;
@using OA.Core.Other;
@using OA.Core.Model;
@using OA.BLL; 
@using OA.AspNetCore.Html;
@using OA.Core.Helper;
@model OA.Model.Voucher
@{
    CustomForm infoCustomForm = ViewBag.CustomForm;
    ViewBag.Title = Model.Title + "---" + infoCustomForm.Name;

    CustomFormExtendInfo infoCustomFormExtendInfo = ViewBag.CustomFormExtendInfo;
    //Dictionary<string, string> dic = Model.ExtensionResultInfo.DicExtend;
    Layout = "~/Areas/Member/Views/Shared/_Layout.cshtml";
    Approval infoApproval = ViewBag.InfoApproval;
    ApprovalItem approvalItem = ViewBag.InfoApprovalItem;
    List<string> enableEditableFields = ViewBag.EnableEditableFields;

    List<ListItem> listConducted = ViewBag.Conducted;
    ApprovalResult approvalResult = ViewBag.ApprovalResult;

    List<string> privacyFields = ViewBag.PrivacyFields;//隐私字段

    List<ExtendField> ExtendFields = ViewBag.ExtendFields;
    var tables = ExtendFields.Where(m => m.Type == 99).ToList();

    var references = ExtendFields.Where(m => m.Type == 30 || m.Type == 31).ToList();

}
<style type="text/css">
    .layui-table-header {
        text-align: justify;
        text-align-last: justify;
    }
</style>
<div class="layui-my-content">
    <p style="width:100%;display:block;font-size:24px;font-weight:bold;text-decoration:underline;text-align:center;">@infoCustomForm.Name</p>
    <form class="layui-form myFrom">
        <table class="layui-table">
            <tr>
                <td class="layui-table-header" style="width:85px;">@Html.GetMyLabelControl(model => model.Title)</td>
                <td>
                    @if (enableEditableFields.Contains("Title"))
                    {
                        @Html.MyEditorFor(model => model.Title)
                    }
                    else
                    {
                        @Model.Title
                    }

                </td>
            </tr>
            <tr>
                <td class="layui-table-header">@Html.GetMyLabelControl(model => model.Sponsor)</td>
                <td>@Model.DepartmentName @Model.PositionName  @Model.SponsorName </td>
            </tr>

            @if (ExtendFields != null && ExtendFields.Count > 0)
            {
                foreach (var item in ExtendFields)
                {
                    if (item.Type == 99 || item.Type == 30 || item.Type == 31) { continue; }
                    string itemValue = string.Empty;
                    if (Model.Result.ContainsKey(item.Attribute))
                    {
                        itemValue = Model.Result[item.Attribute];
                    }
        <tr>
            <td class="layui-table-header">@item.Name</td>
            @if (privacyFields.Contains(item.Attribute))
            {
                @Html.Raw("******")
            }
            else if (enableEditableFields.Contains(item.Attribute))
            {
                item.Value = itemValue;
                @Html.Raw("<td>" + MvcHtmlExtension.GetMyEditorControl(item, "dicExtend.") + "</td>")
            }
            else
            {
                @if (item.Type == 15 || item.Type == 16)
                {
                    <td class="info-content">@Html.Raw($"<a href='itemValue' target='_blank'>{CommonHelper.GetFileName(itemValue)}</a>")</td>
                }
                else if (item.Type == 17 || item.Type == 18)
                {
                    List<string> files = JsonHelper.DeserializeList<string>(itemValue);
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    for (int i = 0; i < files.Count; i++)
                    {
                        sb.Append($"<a href='{files[i]}' target='_blank'>{CommonHelper.GetFileName(files[i])}</a> ");
                    }
                    <td class="info-content">@Html.Raw(sb.ToString())</td>
                }
                else
                {
                    <td class="info-content">@itemValue</td>
                }
            }

        </tr>
                }
            }
            @foreach (var item in references)
            {
                var simpleApprovals = JsonHelper.DeserializeObj<List<SimpleApproval>>(Model.Result[item.Attribute]);
                if (simpleApprovals != null && simpleApprovals.Count > 0)
                {
                    <tr>
                        <td>@item.Name</td>
                        <td><table class="layui-table">
                                <thead><tr><th>单据编号</th><th>单据标题</th></tr></thead>
                            <tbody>
                                @foreach (var simpleApproval in simpleApprovals)
                                {
                                    <tr><td>@simpleApproval.No</td><td>@simpleApproval.Title</td></tr>
                                }
                            </tbody>
                            </table></td>
                    </tr>
                }

            }
            @foreach (var item in tables)
            {
                <tr>
                    <td>@item.Name</td>
                    <td>
                        @{
                            List<Dictionary<string, string>> tableValues = new List<Dictionary<string, string>>();
                            if (Model.Result.ContainsKey(item.Attribute))
                            {
                                string tableValueString = Model.Result[item.Attribute];
                                if (!string.IsNullOrWhiteSpace(tableValueString))
                                {
                                    tableValues = JsonHelper.DeserializeList<Dictionary<string, string>>(tableValueString);
                                }
                            }
                            if (tableValues == null || tableValues.Count == 0)
                            {
                                tableValues = new List<Dictionary<string, string>>() { new Dictionary<string, string>() };
                            }
                            ViewDataDictionary keyValuePairs = new ViewDataDictionary(ViewData);

                            keyValuePairs["TableInfo"] = item.DefaultValue;
                            keyValuePairs["TableAttribute"] = item.Attribute;
                            keyValuePairs["TableName"] = item.Name;
                            keyValuePairs["PrivacyFields"] = privacyFields;
                            keyValuePairs["EnableEditableFields"] = enableEditableFields;
                            @Html.Partial("TableInfoView", tableValues, keyValuePairs)
                        }
                    </td>
                </tr>
            }

            <tr>
                <td colspan="2" style="padding:0px;">
                    <table class="layui-table" style="margin:0px;">
                        <thead>
                            <tr><td colspan="6" style="text-align:center;">审批流程 <span style="text-align:right;display:inline-block;float:right;">申请时间:@Model.ApplicationDate.ToString2()</span></td></tr>
                            <tr>
                                <td style="width:30px;">步骤</td>
                                <td>审批人</td>
                                <td>审批意见</td>
                                <td>说明</td>
                                <td>审批时间</td>
                            </tr>
                        </thead>
                        <tbody>
                        <tbody>
                            @foreach (ApprovalItem item in approvalResult.approvalItems)
                            {
                                string userName = "";
                                <tr>
                                    <td style="text-align:center;">@item.Step</td>
                                    <td>
                                        @if (!string.IsNullOrWhiteSpace(item.RealEmpID) && item.EmpID != item.RealEmpID)
                                        {
                                            userName = item.DepartmentName + " " + item.PositionName + " " + item.EmpName + $"（{item.RealEmpName} 代办）";
                                        }
                                        else
                                        {
                                            userName = item.DepartmentName + " " + item.PositionName + " " + item.EmpName;
                                        }
                                        @userName
                                    </td>
                                    <td>@item.StatusName</td>
                                    <td>@item.Explain</td>
                                    <td>@item.ApprovalTime.ToString2()</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </td>
            </tr>
            @if (approvalItem.Status <= 1)
            {
                <tr>
                    <td rowspan="2">审批选项</td>
                    <td>
                        <input type="radio" name="Status" value="4" title="同意">
                        <input type="radio" name="Status" value="5" title="需沟通">
                        <input type="radio" name="Status" value="6" title="不同意">
                        <input type="radio" name="Status" value="7" title="转移">
                    </td>
                </tr>
                <tr>
                    <td>
                        <textarea placeholder="请输入补充说明,如无可不填写" id="Explain" name="Explain" class="layui-textarea"></textarea>
                    </td>
                </tr>
            }
            @if (listConducted != null && listConducted.Count > 0 && approvalItem.Status <= 0)
            {
                <tr>
                    <td class="layui-table-header">操作</td>
                    <td style="text-align:right;padding-right:12px;">
                        @if (infoApproval.Status <= 1)
                        {
                            if (listConducted != null && listConducted.Count > 0)
                            {
                                foreach (ListItem item in listConducted)
                                {
                                    <input type="button" name="RemoveEmpID" data-text="@item.Text" value="返回至：@item.Text" data-value="@item.Value" class="layui-btn remove" />
                                }
                            }
                            <input type="button" value="提交" class="layui-btn mySubmit" />
                        }
                    </td>
                </tr>
            }
            else
            {
                <tr>
                    <td class="layui-table-header">操作</td>
                    <td style="text-align:right;padding-right:12px;">
                        @if (approvalItem.Status <= 1)
                        {
                            <input type="button" value="提交" class="layui-btn mySubmit" />
                        }
                    </td>
                </tr>
            }

        </table>
    </form>
</div>
<script type="text/javascript">
    layui.use(['form', 'element', 'layer', 'element'], function () {
        form = layui.form;
        layer = layui.layer;
        element = layui.element;
    });
    var inputType = [1, 2, 6, 8, 9, 10, 11, 12, 15, 16, 19, 20, 21, 30, 40, 41, 42, 43, 44, 68, 69, 81, 82, 83];
    var selectType = [3, 25];
    var radioType = [4];
    var checkboxType = [5, 7];
    var pointType=@approvalItem.PointType;
    $(".mySubmit").click(function () {
        $(".mytablecontent").each(function (index, item) {
            var attribute = $(item).attr("data-tableattribute");
            var list = [];
            $(item).find("tbody tr").each(function (trIndex, trItem) {
                var trInfo = {};
                $(trItem).find("td").each(function (tdIndex, tdItme) {

                    var tdttribute = $(tdItme).attr("data-attribute");
                    console.log(tdttribute);
                    if (tdttribute != undefined && tdttribute!=null) {
                        var type = parseInt($(tdItme).attr("data-type"));

                        var tdValue = "";
                        if (inputType.includes(type)) {
                            tdValue = $(tdItme).find("input").val();
                        }
                        else if (selectType.includes(type)) {

                            tdValue = $(tdItme).find("select").val();
                        }
                        else if (radioType.includes(type)) {
                            tdValue = $(tdItme).find('input:radio:checked').val();
                        }
                        else if (checkboxType.includes(type)) {
                            var vals = [];
                            $.each($(tdItme).find('input:checkbox:checked'), function () {
                                vals.push($(this).val());
                            });
                            tdValue = vals.join(',');

                        }
                        else if (type == 23) {
                            $.each($(tdItme).find('.Department'), function () {
                                if ($(this).val() != "0") {
                                    tdValue = $(this).val();
                                }
                            });
                        }
                        else if (type == 22 || type == 24) {
                            tdValue = $(tdItme).find('select').last().val();
                            if (type == 22) {
                                tdValue = Base64.decode(tdValue);
                            }
                        }

                        else {
                            console.log(type + "未设置值接收");
                        }
                        trInfo[tdttribute] = tdValue;
                    }

                });
                list.push(trInfo);
            });
            $("#dicExtend_" + attribute).val(JSON.stringify(list));
        })
        if (pointType == 1) {
            if ($('[name=Status]:checked').length == 0) {
                layer.msg("请您勾选一个审批选项");
                return;
            }
        }
        else {
            if ($.trim($("#Explain").val()).length==0) {
                layer.msg("请填写会签意见");
                return;
            }
        }
        layer.confirm('请确认您提交的信息？', {
            btn: ['已确认,并提交', '不小心点错了'] //按钮
        }, function () {
            $.ajax({
            url: "/Member/Home/Conduct/@approvalItem.ID",
            type: "Post",
            data: $(".myFrom").serialize(),
            dataType: "json",
            beforeSend: function () {
                layer.msg("正在提交，请稍后...");
                $(".mySubmit").prop('disabled', true);
            },
            success: function (data) {
                if (data.State == 400) {
                    $(".mySubmit").prop('disabled', false);
                    layer.msg(data.Msg);
                    return;
                }
                if (data.State == 402) {
                    $(".mySubmit").prop('disabled', false);
                    layer.msg(data.Msg);
                    return;
                }
                else {
                    window.location.href = "/Member/Home/Conduct/@approvalItem.ID";
                }
            }
        });

        }, function () {

        });


    });
    $(".remove").click(function () {
        var empName = $(this).attr("data-text");
        var positionID = $(this).attr("data-value");
        var explain = $.trim($("#Explain").val());
        var objBtn = $(this);
        layer.confirm('您确认将单据转至' + empName+'?', {
            btn: ['是的，我要转移', '不小心点错了'] //按钮
        }, function () {
            objBtn.prop('disabled', true);
            var url = "/Member/Home/Remove/@infoApproval.ID";
            $.post(url, { positionID: positionID, explain: explain },
            function(data){
                if (data.State == 200) {
                    window.location.href = "/Member/Home/Conduct/@infoApproval.ID";
                }
                else
                {
                    $(".mySubmit").prop('disabled', false);
                    layer.msg(data.Msg);
                }
            });
        }, function () {

        });
    });
</script>
